﻿using System;
using Gumini.JulaDB.Provider;
using Gumini.JulaDB.Test.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Gumini.JulaDB.Test
{
    /// <summary>
    /// A set of tests for use of null value in expressions.
    /// </summary>
    [TestClass]
    public class NullTest
    {
        private static JulaDbConnection _connection;

        [ClassInitialize]
        public static void InitializeDatabase(TestContext context)
        {
            _connection = TestUtils.CreateTestDatabase();
        }

        [ClassCleanup]
        public static void CleanupDatabase()
        {
            _connection.Close();
        }

        /// <summary>
        /// Simple select of a null value.
        /// </summary>
        [TestMethod]
        public void SimpleSelectNull()
        {
            _connection.ExecuteValidScalar("select null from dual", DBNull.Value);
        }

        /// <summary>
        /// Use of null in an arithetic expression. Expression should evaluate to null.
        /// </summary>
        [TestMethod]
        public void NullInArithmeticExpression()
        {
            _connection.ExecuteValidScalar("select (2*3) + null / 12 from dual", DBNull.Value);
        }

        /// <summary>
        /// Use of null in a boolean OR expression. Here, expression should evaluate to true.
        /// </summary>
        [TestMethod]
        public void NullInBooleanExpression()
        {
            _connection.ExecuteValidScalar("select (1 = 1) OR null from dual", true);
        }

        /// <summary>
        /// Comparing two nulls for equality should return null.
        /// </summary>
        [TestMethod]
        public void NullEqualComparisonUnknown()
        {
            _connection.ExecuteValidScalar("select (NULL = NULL) from dual", DBNull.Value);
        }

        /// <summary>
        /// Comparing two nulls for inequality should return null.
        /// </summary>
        [TestMethod]
        public void NullNotEqualComparisonUnknown()
        {
            _connection.ExecuteValidScalar("select (NULL != NULL) from dual", DBNull.Value);
        }

        /// <summary>
        /// A 'less than' comparison of two nulls should return null.
        /// </summary>
        [TestMethod]
        public void NullLesserThanComparisonUnknown()
        {
            _connection.ExecuteValidScalar("select NULL < NULL from dual", DBNull.Value);
        }
    }
}
